import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:shenling_wuliu/component/content.dart';
import 'package:shenling_wuliu/utils/http.dart';
import 'package:shenling_wuliu/utils/toast.dart';

class MineWidget extends StatefulWidget {
   MineWidget({super.key,required this.currentIndex});
  int currentIndex;
  @override
  State<MineWidget> createState() => __MineWidgetState();
}

class __MineWidgetState extends State<MineWidget> {
  //本月任务列表
  List monthTask = [
    {
      "number": "18",
      "title": "任务总量"
    },
    {
      "number": "16",
      "title": "任务总量"
    },
    {
      "number": "3987",
      "title": "运输里程(km)"
    },
  ];
  List bottomTaskList = [
    {"title":"车辆信息"},
    {"title":"任务数据"},
    {"title":"系统设置"},
  ];
  //获取7中的任务列表结构
  List<Widget> getMonthTask() {
    List<Widget> list = [];
    
     list.addAll(
      monthTask.map((item) {
        return Column(
          children: [
            Text(item['number'],style: TextStyle(fontSize: 14,fontWeight: FontWeight.bold)),
            SizedBox(height: 5,),
            Text(item['title'],style: TextStyle(fontSize: 14))
          ],
        );
      }).toList()
     ); 
    return list;
  }

  List<Widget> getBottomList(){
    List<Widget> list = [];
    list.addAll(
      bottomTaskList.map((item){
        return GestureDetector(
          onTap: () {
            getSystem(item['title']);
          },
          child: ContentItem(title: item['title']),
        );
      }).toList()
    );
    return list;
  }
  getSystem(String title) async{
    if(title == '系统设置') {
      Navigator.pushNamed(context, '/system');
    }
  }
  Map userInfo = {
    "avatar": "ic_avatar_driver.svg",
    "name": "李明",
    "number": "67409881",
    "phone": "17279273702"
  };
  @override
  void initState() {
    super.initState();
    //获取用户数据
    // getUserInfo();
  }
  @override
  void didUpdateWidget(covariant MineWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    // print(widget.currentIndex);
    if(widget.currentIndex == 2) {
      //调接口，获取用户数据
      print('lllllllllllllllll');
      getUserInfo();
    }
  }
  getUserInfo() async{
    try{
      var res = await http.get('/driver/users');
      setState(() {
        userInfo = res['data'];
      });
      
    }catch(e){
      ToastUtil.showError('请求我的页面数据失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      // padding: EdgeInsets.all(16),
      color: const Color.fromARGB(255, 244, 243, 243),
      child: Stack(
        alignment: AlignmentDirectional.topCenter,
        children: [
          //1：背景图
          Image.asset('bg_page_my.jpg',width: MediaQuery.of(context).size.width),
          Column(
            children: [
              SizedBox(height: 15,),
              //2:汽车图标
              SvgPicture.asset('cart.svg',width: 50),
              SizedBox(height: 15),
              //3: 头像
              Container(
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(30)
                ),
                child: userInfo['avatar'] == null ? SvgPicture.asset('ic_avatar_driver.svg',width: 60) : Image.network(userInfo['avatar'],width: 60),
              ),
              //4: 昵称
              SizedBox(height: 8),
              Text(userInfo['name'],style: TextStyle(color: Colors.white,fontSize: 18)),
              //5: 司机编号
              SizedBox(height: 8),
              Text('司机编号: ${userInfo['number']}',style: TextStyle(color: Colors.white,fontSize: 12)),
              //6:手机号
              SizedBox(height: 5),
              Text('手机号: ${userInfo['phone']}',style: TextStyle(color: Colors.white,fontSize: 12)),
              // SizedBox(height: 10),
              //7：本月任务
              Container(
                margin: EdgeInsets.all(10),
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          //7.1：本月任务文本
                          Text('- 本月任务 -',style: TextStyle(color: Colors.grey),),
                          SizedBox(height: 10),
                          //7.2：本月任务列表
                          Row(
                              //7.2.1:任务总量
                              //7.2.2:完成任务量
                              //7.2.3:运输里程(km)
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              children: getMonthTask()
                          )
                        ],
                      ),
              ),
              //8:底部信息
              Container(
                margin: EdgeInsets.all(10),
                padding: EdgeInsets.all(10),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(10),
                ),
                child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: getBottomList(),
              ),
              )
            ],
          )
        ],
      ),
    );
  }
}